Naive Operations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 2438 Accepted Submission(s): 1074
Problem Description
In a galaxy far, far away, there are two integer sequence a and b of length n.
b is a static permutation of 1 to n. Initially a is filled with zeroes.
There are two kind of operations:
- add l r: add one for al,al+1…ar
- query l r: query ∑ri=l⌊ai/bi⌋
Input
There are multiple test cases, please read till the end of input file.
For each test case, in the first line, two integers n,q, representing the length of a,b and the number of queries.
In the second line, n integers separated by spaces, representing permutation b.
In the following q lines, each line is either in the form ‘add l r’ or ‘query l r’, representing an operation.
1≤n,q≤100000, 1≤l≤r≤n, there’re no more than 5 test cases.
Output
Output the answer for each ‘query’, each one line.
Sample Input
5 12
1 5 2 4 3
add 1 4
query 1 4
add 2 5
query 2 5
add 3 5
query 1 5
add 2 4
query 1 4
add 2 5
query 2 5
add 2 2
query 1 5
Sample Output
1
1
2
4
4
6
Source
2018 Multi-University Training Contest 2
比赛的时候写了半天没写出来,结果发现是线段树板子敲错了-_-|||
给一段区间,区间的值全部加+1
查询 区间 a[i]/b[i]向下取整的和。
因为查询a[i]/b[i]向下取整,直接求有点难。
所以我们换个操作,我们每次区间加一,变成把每个值减一,每次减到0的时候ai/bi的值就会+1,我们记录这个+1,再把值重新更新为bi,查询的时候查询+1 的总和。
用线段树保留最小值,当出现最小值为0的时候把cnt++,值更新为b[r],因为每次只会加+1所以总数不会太大
如: 1 2 3 4 5
add 1 4
区间值 0 1 2 3 5 ,出现最小值 0 所以区间 cnt++ 把零变为 b[i]
1 1 2 3 5
然后一直下去,查询直接查询cnt 总和就行。
1 | #include<iostream> |